package com.kang.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.kang.dao.StorageDistributeMapper;
import com.kang.pojo.ItemDetail;
import com.kang.pojo.StorageDistribute;
import com.kang.pojo.Storagedistribte_iteminformation;
import com.kang.service.StorageDistributeService;
import com.vonechina.entity.page.ItemDetailPage;
import com.vonechina.entity.page.StorageDistributePage;

/**
 * 库存调配服务impl
 * 
 * @author ysq
 *
 */
@Service
public class StorageDistributeServiceImpl implements StorageDistributeService {
	@Resource
	private StorageDistributeMapper storageDistributeMapper;

	/**
	 * 根据storageDistributeID删除storagedistribute库存调配
	 */
	@Override
	public int deleteStoragedistribute(Integer storageDistributeID) {
		return storageDistributeMapper
				.deleteStoragedistribute(storageDistributeID);
	}

	/**
	 * 根据storageDistributeID查询storagedistribute库存调配
	 */
	@Override
	public StorageDistribute selectStoragedistributeByStorageDistributeID(
			int storageDistributeID) {
		return storageDistributeMapper
				.selectStoragedistributeByStorageDistributeID(storageDistributeID);
	}

	/**
	 * 查询StorageDistribute库存调配
	 */
	@Override
	public List<Map<?, ?>> selectByWheres(
			StorageDistributePage storageDistributePage) {
		// 查询StorageDistribute库存调配总条数
		storageDistributePage.setRows(storageDistributeMapper
				.selectByWheresSum(storageDistributePage));
		return storageDistributeMapper.selectByWheres(storageDistributePage);
	}

	/**
	 * 添加库存调配信息
	 */
	@Override
	public int addStorageDistribute(StorageDistribute storageDistribute) {
		return storageDistributeMapper.addStorageDistribute(storageDistribute);
	}

	/**
	 * 修改库存调配信息
	 */
	@Override
	public int updateStorageDistribute(StorageDistribute storageDistribute) {
		return storageDistributeMapper
				.updateStorageDistribute(storageDistribute);
	}

	/**
	 * 生成库存调配单号
	 */
	@Override
	public String generateDistributeNumber() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String text = sdf.format(new Date()) + "0001";
		Long no = Long.parseLong(text);
		String max = storageDistributeMapper.selectMaxDistributeNumber();
		Long l = 0l;
		if (max != null) {
			l = Long.parseLong(max.replaceAll("[a-zA-Z]", ""));
		}
		if (l >= no) {
			no = l + 1;
		}
		return "TP" + no;
	}

	/**
	 * 查询item_detail表
	 */
	@Override
	public List<Map<?, ?>> selectItemDetailList(ItemDetailPage itemDetailPage) {
		// 查询总条数
		itemDetailPage.setRows(storageDistributeMapper
				.selectItemDetailListSum(itemDetailPage));
		return storageDistributeMapper.selectItemDetailList(itemDetailPage);
	}

	/**
	 * 保存库存调配详情
	 */
	@Override
	public int addStoragedistribte_iteminformation(
			Storagedistribte_iteminformation storagedistribte_iteminformation) {
		return storageDistributeMapper
				.addStoragedistribte_iteminformation(storagedistribte_iteminformation);
	}

	/**
	 * 根据id查询出item_detail
	 */
	@Override
	public ItemDetail selectItem_detailById(Integer id) {
		return storageDistributeMapper.selectItem_detailById(id);
	}

	/**
	 * 添加item_detail
	 */
	@Override
	public Integer addItem_detail(ItemDetail item_detail) {
		return storageDistributeMapper.addItem_detail(item_detail);
	}

	/**
	 * 根据id修改item_detail数量
	 */
	@Override
	public Integer updateItem_detail(Integer item_detailId, Integer number) {
		return storageDistributeMapper.updateItem_detail(item_detailId, number);
	}

	/**
	 * 根据批次号和仓库id查询出数据库是否已经存在数据，存在则修改，否则添加
	 */
	@Override
	public ItemDetail selectItem_detailByCondition(String batchNumber,
			Integer warehouseManagementID) {
		return storageDistributeMapper.selectItem_detailByCondition(
				batchNumber, warehouseManagementID);
	}

	/**
	 * 根据id修改item_detail数量 加上原有数量
	 */
	@Override
	public Integer updateItem_detailNumber(Integer item_detailId, Integer number) {
		return storageDistributeMapper.updateItem_detailNumber(item_detailId,
				number);
	}

	/**
	 * 根据storageDistributeID查询库存调配详情_物品信息
	 */
	@Override
	public List<Map<?, ?>> selectStoragedistribteIteminformationByStorageDistributeID(
			Integer storageDistributeID) {
		return storageDistributeMapper
				.selectStoragedistribteIteminformationByStorageDistributeID(storageDistributeID);
	}

	/**
	 * 根据storageDistribteID删除所有storagedistribte_iteminformation信息
	 */
	@Override
	public Integer delStoragedistribteIteminformation(Integer storageDistribteID) {
		return storageDistributeMapper
				.delStoragedistribteIteminformation(storageDistribteID);
	}
}
